import JsonManager from "../manager/json_manager";
import StorageManager from "../manager/storage_manager";
import Enemy from "./Enemy";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Car extends cc.Component {


    atk: number = 10
    speed: number = 200;

    onLoad() { this.atk = JsonManager.instance.getDataByName('skill')[7].damage }



    update(dt) {
        // this.node.position = dt*cc.director.getScheduler().getTimeScale()


        // 计算每帧需要移动的距离
        const moveDistance = this.speed * dt * cc.director.getScheduler().getTimeScale();

        // 修改节点的 y 坐标
        this.node.y += moveDistance;
    }

     calculateValue(baseValue) {
        let count = StorageManager.instance.carCount
        // 计算减少的百分比，最低限制为 40%
        const percentage = Math.max(40, 100 - count * 15);
        // 返回减少后的值
        return Math.ceil(baseValue * (percentage / 100)) ;
    }

    onCollisionEnter(other, self) {
        // cc.log('bullet 打击Enter');
        if (other.node.group === 'Enemy') {

            let enemyScript = other.getComponent(Enemy)
            // this.attackTarget(other.getComponent(Enemy));

            if (enemyScript) {
                cc.log('车伤'+this.calculateValue(this.atk));
                enemyScript.takeDamage(this.calculateValue(this.atk) ,true); // 对敌人造成伤害
                //  cc.log("扩散次数:" + this.spreadCount)
            }
         
        }
    }
}
